package edu.cuit.zhuyimeng.flight.convert;

import cn.hutool.extra.spring.SpringUtil;
import edu.cuit.zhuyimeng.aircraft.api.controller.AircraftQueryController;
import edu.cuit.zhuyimeng.aircraft.api.dto.AircraftInfoDTO;
import edu.cuit.zhuyimeng.flight.controller.dto.CreateFlightDTO;
import edu.cuit.zhuyimeng.flight.controller.vo.FlightInfoVO;
import edu.cuit.zhuyimeng.flight.dal.po.FlightPO;
import edu.cuit.zhuyimeng.framework.common.exception.ServerException;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.framework.common.system.enums.FlightStatus;
import edu.cuit.zhuyimeng.framework.common.system.enums.SysConstants;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;

import java.util.Objects;

/**
 * 航班对象转换工具类
 */
public class FlightConverter {

    public static FlightPO createFlightDTOToFlightPO(CreateFlightDTO createFlightDTO) {
        return FlightPO.builder()
                .aircraftId(createFlightDTO.getAircraftId())
                .number(createFlightDTO.getNumber())
                .deployCity(createFlightDTO.getDeployCity())
                .arrivalCity(createFlightDTO.getArrivalCity())
                .startTime(createFlightDTO.getStartTime())
                .endTime(createFlightDTO.getEndTime())
                .startBook(createFlightDTO.getStartBook())
                .endBook(createFlightDTO.getEndBook())
                .price(createFlightDTO.getPrice())
                .build();
    }

    public static FlightInfoVO convertToFlightInfoVO(FlightPO flightPO) {
        AircraftQueryController aircraftQueryController = SpringUtil.getBean(AircraftQueryController.class);
        CommonResult<AircraftInfoDTO> aircraftInfo = aircraftQueryController.getAircraftInfo(flightPO.getAircraftId());

        if (aircraftInfo.isError()) {
            throw new ServerException("查询飞机信息失败: " + aircraftInfo.getMsg());
        }

        RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);
        FlightStatus flightStatus;
        try {
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(String.valueOf(flightPO.getId())).includeProcessVariables().singleResult();
            flightStatus = FlightStatus.getFlightStatusByType((Integer) processInstance.getProcessVariables().get(SysConstants.STATUS));
        } catch (Exception e) {
            flightStatus = FlightStatus.PREPARING;
        }
        if (flightStatus == null) {
            flightStatus = FlightStatus.PREPARING;
        }
        return FlightInfoVO.builder()
                .id(flightPO.getId())
                .aircraftInfoDTO(aircraftInfo.getData())
                .number(aircraftInfo.getData().getAirlineCode() + flightPO.getNumber())
                .arrivalCity(flightPO.getArrivalCity())
                .deployCity(flightPO.getDeployCity())
                .startTime(flightPO.getStartTime())
                .endTime(flightPO.getEndTime())
                .startBook(flightPO.getStartBook())
                .endBook(flightPO.getEndBook())
                .price(flightPO.getPrice())
                .status(flightStatus.getType())
                .statusInfo(flightStatus.getInfo())
                .build();
    }

}
